package com.zbkj.service.service.adminregion.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zbkj.common.model.product.Product;
import com.zbkj.common.model.product.ProductCategory;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.region.SearchRegionVo;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.ProductSearchRequest;
import com.zbkj.common.request.ProductTabsHeaderRequest;
import com.zbkj.common.response.PlatformProductListResponse;
import com.zbkj.common.response.ProductTabsHeaderResponse;
import com.zbkj.service.dao.ProductDao;
import com.zbkj.service.helper.region.RegionHelper;
import com.zbkj.service.service.ProductCategoryService;
import com.zbkj.service.service.adminregion.IRegionProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: RegionProductServiceImpl
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/8/17 14:13
 * @Version: V1.0
 **/
@Service
@Slf4j
public class RegionProductServiceImpl implements IRegionProductService {
    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private RegionHelper regionHelper;

    /**
     * 区域端商品分页列表
     *
     * @param request          查询参数
     * @param pageParamRequest 分页参数
     * @return PageInfo
     */
    @Override
    public PageInfo<PlatformProductListResponse> getRegionPageList(ProductSearchRequest request, PageParamRequest pageParamRequest) {
        SearchRegionVo currentUserRegion = regionHelper.getCurrentUserRegion();
        if (currentUserRegion.isMerchantIdNullFlag()) {
            return new PageInfo<>();
        }

        HashMap<String, Object> map = Maps.newHashMap();
        map.put("merIds", currentUserRegion.getMerchantIds());
        map.put("type", request.getType());
        if (ObjectUtil.isNotNull(request.getCategoryId())) {
            ProductCategory category = productCategoryService.getById(request.getCategoryId());
            if (category.getLevel().equals(3)) {
                map.put("categoryIds", request.getCategoryId());
            } else {
                List<ProductCategory> categoryList = productCategoryService.findAllChildListByPid(category.getId(), category.getLevel());
                List<String> cateIdList = categoryList.stream().filter(e -> e.getLevel().equals(3)).map(e -> e.getId().toString()).collect(Collectors.toList());
                String categoryIds = String.join(",", cateIdList);
                map.put("categoryIds", categoryIds);
            }
        }
        if (ObjectUtil.isNotNull(request.getIsSelf())) {
            map.put("self", request.getIsSelf());
        }
        if (StrUtil.isNotBlank(request.getKeywords())) {
            String keywords = URLUtil.decode(request.getKeywords());
            map.put("keywords", keywords);
        }
        if (ObjectUtil.isNotNull(request.getIsPaidMember())) {
            map.put("isPaidMember", request.getIsPaidMember() ? 1 : 0);
        }
        if (ObjectUtil.isNotNull(request.getProductType())) {
            map.put("productType", request.getProductType());
        }
        Page<Product> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<PlatformProductListResponse> proList = productDao.getRegionPageList(map);
        return CommonPage.copyPageInfo(page, proList);
    }


    /**
     * 区域端获取商品表头数量
     *
     * @return List
     */
    @Override
    public List<ProductTabsHeaderResponse> getRegionTabsHeader(ProductTabsHeaderRequest request) {
        SearchRegionVo currentUserRegion = regionHelper.getCurrentUserRegion();
        if (currentUserRegion.isMerchantIdNullFlag()) {
            return Lists.newArrayList();
        }


        List<ProductTabsHeaderResponse> headers = new ArrayList<>();
        ProductTabsHeaderResponse header1 = new ProductTabsHeaderResponse(0, 1);
        ProductTabsHeaderResponse header2 = new ProductTabsHeaderResponse(0, 2);
        ProductTabsHeaderResponse header6 = new ProductTabsHeaderResponse(0, 6);
        ProductTabsHeaderResponse header7 = new ProductTabsHeaderResponse(0, 7);
        headers.add(header1);
        headers.add(header2);
        headers.add(header6);
        headers.add(header7);

        HashMap<String, Object> map = CollUtil.newHashMap();
        map.put("merIds", currentUserRegion.getMerchantIds());
        if (ObjectUtil.isNotNull(request.getCategoryId())) {
            ProductCategory category = productCategoryService.getById(request.getCategoryId());
            if (category.getLevel().equals(3)) {
                map.put("categoryIds", request.getCategoryId());
            } else {
                List<ProductCategory> categoryList = productCategoryService.findAllChildListByPid(category.getId(), category.getLevel());
                List<String> cateIdList = categoryList.stream().filter(e -> e.getLevel().equals(3)).map(e -> e.getId().toString()).collect(Collectors.toList());
                String categoryIds = String.join(",", cateIdList);
                map.put("categoryIds", categoryIds);
            }
        }

        if (ObjectUtil.isNotNull(request.getIsSelf())) {
            map.put("self", request.getIsSelf());
        }
        if (StrUtil.isNotBlank(request.getKeywords())) {
            String keywords = URLUtil.decode(request.getKeywords());
            map.put("keywords", keywords);
        }
        if (ObjectUtil.isNotNull(request.getIsPaidMember())) {
            map.put("isPaidMember", request.getIsPaidMember() ? 1 : 0);
        }
        if (ObjectUtil.isNotNull(request.getProductType())) {
            map.put("productType", request.getProductType());
        }
        for (ProductTabsHeaderResponse h : headers) {
            map.put("type", h.getType());
            h.setCount(productDao.getRegionPageCount(map));
        }
        return headers;
    }
}
